﻿//**************************************************
//CLASS : tcdc_mst_user
//AUTHOR : PATTARAWUT CHANNGAM & SUCHIN INPIRUT
//LAST MODIFIED : Monday, February 25, 2013
//**************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Model
{

public class tcdc_mst_user
{

     private DBUtil DB;
     public string strConn { get; set; }
     public int user_id {get;set;}
     public int user_type_id {get;set;}
     public string username {get;set;}
     public string password {get;set;}
     public int title_th {get;set;}
     public string fname_th {get;set;}
     public string lname_th {get;set;}
     public int title_en {get;set;}
     public string fname_en {get;set;}
     public string lname_en {get;set;}
     public string id_card {get;set;}
     public string tag {get;set;}
     public string birth_date {get;set;}
     public string face_image {get;set;}
     public string gender {get;set;}
     public string email {get;set;}
     public string telephone {get;set;}
     public string mobile {get;set;}
     public string role {get;set;}
     public string lang {get;set;}
     public string create_date {get;set;}
     public int create_by {get;set;}
     public string update_date {get;set;}
     public int update_by {get;set;}
     public int site_id {get;set;}

     public tcdc_mst_user() {
          this.DB = new DBUtil();
          DB.strConn = strConn;
     }

     public DataTable getById(string id) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_user" + 
                       " WHERE user_id = '" + id + "'";

          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public DataTable getByObject(tcdc_mst_user obj) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_user " + 
                       "WHERE 1 = 1 ";

          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               sql += "AND user_id LIKE '%" + obj.user_id + "%' ";
          }
          if(obj.user_type_id != null && 
             obj.user_type_id.ToString() != null && 
             obj.user_type_id.ToString() != "0")
          {
               sql += "AND user_type_id LIKE '%" + obj.user_type_id + "%' ";
          }
          if(obj.username != null && 
             obj.username.ToString() != null && 
             obj.username.ToString() != "0")
          {
               sql += "AND username LIKE '%" + obj.username + "%' ";
          }
          if(obj.password != null && 
             obj.password.ToString() != null && 
             obj.password.ToString() != "0")
          {
               sql += "AND password LIKE '%" + obj.password + "%' ";
          }
          if(obj.title_th != null && 
             obj.title_th.ToString() != null && 
             obj.title_th.ToString() != "0")
          {
               sql += "AND title_th LIKE '%" + obj.title_th + "%' ";
          }
          if(obj.fname_th != null && 
             obj.fname_th.ToString() != null && 
             obj.fname_th.ToString() != "0")
          {
               sql += "AND fname_th LIKE '%" + obj.fname_th + "%' ";
          }
          if(obj.lname_th != null && 
             obj.lname_th.ToString() != null && 
             obj.lname_th.ToString() != "0")
          {
               sql += "AND lname_th LIKE '%" + obj.lname_th + "%' ";
          }
          if(obj.title_en != null && 
             obj.title_en.ToString() != null && 
             obj.title_en.ToString() != "0")
          {
               sql += "AND title_en LIKE '%" + obj.title_en + "%' ";
          }
          if(obj.fname_en != null && 
             obj.fname_en.ToString() != null && 
             obj.fname_en.ToString() != "0")
          {
               sql += "AND fname_en LIKE '%" + obj.fname_en + "%' ";
          }
          if(obj.lname_en != null && 
             obj.lname_en.ToString() != null && 
             obj.lname_en.ToString() != "0")
          {
               sql += "AND lname_en LIKE '%" + obj.lname_en + "%' ";
          }
          if(obj.id_card != null && 
             obj.id_card.ToString() != null && 
             obj.id_card.ToString() != "0")
          {
               sql += "AND id_card LIKE '%" + obj.id_card + "%' ";
          }
          if(obj.tag != null && 
             obj.tag.ToString() != null && 
             obj.tag.ToString() != "0")
          {
               sql += "AND tag LIKE '%" + obj.tag + "%' ";
          }
          if(obj.birth_date != null && 
             obj.birth_date.ToString() != null && 
             obj.birth_date.ToString() != "0")
          {
               sql += "AND birth_date LIKE '%" + obj.birth_date + "%' ";
          }
          if(obj.face_image != null && 
             obj.face_image.ToString() != null && 
             obj.face_image.ToString() != "0")
          {
               sql += "AND face_image LIKE '%" + obj.face_image + "%' ";
          }
          if(obj.gender != null && 
             obj.gender.ToString() != null && 
             obj.gender.ToString() != "0")
          {
               sql += "AND gender LIKE '%" + obj.gender + "%' ";
          }
          if(obj.email != null && 
             obj.email.ToString() != null && 
             obj.email.ToString() != "0")
          {
               sql += "AND email LIKE '%" + obj.email + "%' ";
          }
          if(obj.telephone != null && 
             obj.telephone.ToString() != null && 
             obj.telephone.ToString() != "0")
          {
               sql += "AND telephone LIKE '%" + obj.telephone + "%' ";
          }
          if(obj.mobile != null && 
             obj.mobile.ToString() != null && 
             obj.mobile.ToString() != "0")
          {
               sql += "AND mobile LIKE '%" + obj.mobile + "%' ";
          }
          if(obj.role != null && 
             obj.role.ToString() != null && 
             obj.role.ToString() != "0")
          {
               sql += "AND role LIKE '%" + obj.role + "%' ";
          }
          if(obj.lang != null && 
             obj.lang.ToString() != null && 
             obj.lang.ToString() != "0")
          {
               sql += "AND lang LIKE '%" + obj.lang + "%' ";
          }
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               sql += "AND create_date LIKE '%" + obj.create_date + "%' ";
          }
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               sql += "AND create_by LIKE '%" + obj.create_by + "%' ";
          }
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               sql += "AND update_date LIKE '%" + obj.update_date + "%' ";
          }
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               sql += "AND update_by LIKE '%" + obj.update_by + "%' ";
          }
          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               sql += "AND site_id LIKE '%" + obj.site_id + "%' ";
          }
          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public int insertData(tcdc_mst_user obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[25];

          prm[0] = new SqlParameter("@user_type_id", SqlDbType.Int);
          if(obj.user_type_id != null && 
             obj.user_type_id.ToString() != null && 
             obj.user_type_id.ToString() != "0")
          {
               prm[0].Value = obj.user_type_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }

          prm[1] = new SqlParameter("@username", SqlDbType.NVarChar,50);
          if(obj.username != null && 
             obj.username.ToString() != null && 
             obj.username.ToString() != "0")
          {
               prm[1].Value = obj.username;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }

          prm[2] = new SqlParameter("@password", SqlDbType.NVarChar,50);
          if(obj.password != null && 
             obj.password.ToString() != null && 
             obj.password.ToString() != "0")
          {
               prm[2].Value = obj.password;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }

          prm[3] = new SqlParameter("@title_th", SqlDbType.Int);
          if(obj.title_th != null && 
             obj.title_th.ToString() != null && 
             obj.title_th.ToString() != "0")
          {
               prm[3].Value = obj.title_th;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }

          prm[4] = new SqlParameter("@fname_th", SqlDbType.NVarChar,250);
          if(obj.fname_th != null && 
             obj.fname_th.ToString() != null && 
             obj.fname_th.ToString() != "0")
          {
               prm[4].Value = obj.fname_th;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }

          prm[5] = new SqlParameter("@lname_th", SqlDbType.NVarChar,50);
          if(obj.lname_th != null && 
             obj.lname_th.ToString() != null && 
             obj.lname_th.ToString() != "0")
          {
               prm[5].Value = obj.lname_th;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }

          prm[6] = new SqlParameter("@title_en", SqlDbType.Int);
          if(obj.title_en != null && 
             obj.title_en.ToString() != null && 
             obj.title_en.ToString() != "0")
          {
               prm[6].Value = obj.title_en;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }

          prm[7] = new SqlParameter("@fname_en", SqlDbType.NVarChar,250);
          if(obj.fname_en != null && 
             obj.fname_en.ToString() != null && 
             obj.fname_en.ToString() != "0")
          {
               prm[7].Value = obj.fname_en;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }

          prm[8] = new SqlParameter("@lname_en", SqlDbType.NVarChar,50);
          if(obj.lname_en != null && 
             obj.lname_en.ToString() != null && 
             obj.lname_en.ToString() != "0")
          {
               prm[8].Value = obj.lname_en;
          }
          else
          {
               prm[8].Value = DBNull.Value;
          }

          prm[9] = new SqlParameter("@id_card", SqlDbType.NVarChar,50);
          if(obj.id_card != null && 
             obj.id_card.ToString() != null && 
             obj.id_card.ToString() != "0")
          {
               prm[9].Value = obj.id_card;
          }
          else
          {
               prm[9].Value = DBNull.Value;
          }

          prm[10] = new SqlParameter("@tag", SqlDbType.NVarChar,50);
          if(obj.tag != null && 
             obj.tag.ToString() != null && 
             obj.tag.ToString() != "0")
          {
               prm[10].Value = obj.tag;
          }
          else
          {
               prm[10].Value = DBNull.Value;
          }

          prm[11] = new SqlParameter("@birth_date", SqlDbType.DateTime);
          if(obj.birth_date != null && 
             obj.birth_date.ToString() != null && 
             obj.birth_date.ToString() != "0")
          {
               prm[11].Value = obj.birth_date;
          }
          else
          {
               prm[11].Value = DBNull.Value;
          }

          prm[12] = new SqlParameter("@face_image", SqlDbType.NVarChar,250);
          if(obj.face_image != null && 
             obj.face_image.ToString() != null && 
             obj.face_image.ToString() != "0")
          {
               prm[12].Value = obj.face_image;
          }
          else
          {
               prm[12].Value = DBNull.Value;
          }

          prm[13] = new SqlParameter("@gender", SqlDbType.NVarChar,1);
          if(obj.gender != null && 
             obj.gender.ToString() != null && 
             obj.gender.ToString() != "0")
          {
               prm[13].Value = obj.gender;
          }
          else
          {
               prm[13].Value = DBNull.Value;
          }

          prm[14] = new SqlParameter("@email", SqlDbType.NVarChar,50);
          if(obj.email != null && 
             obj.email.ToString() != null && 
             obj.email.ToString() != "0")
          {
               prm[14].Value = obj.email;
          }
          else
          {
               prm[14].Value = DBNull.Value;
          }

          prm[15] = new SqlParameter("@telephone", SqlDbType.NVarChar,50);
          if(obj.telephone != null && 
             obj.telephone.ToString() != null && 
             obj.telephone.ToString() != "0")
          {
               prm[15].Value = obj.telephone;
          }
          else
          {
               prm[15].Value = DBNull.Value;
          }

          prm[16] = new SqlParameter("@mobile", SqlDbType.NVarChar,50);
          if(obj.mobile != null && 
             obj.mobile.ToString() != null && 
             obj.mobile.ToString() != "0")
          {
               prm[16].Value = obj.mobile;
          }
          else
          {
               prm[16].Value = DBNull.Value;
          }

          prm[17] = new SqlParameter("@role", SqlDbType.NVarChar,20);
          if(obj.role != null && 
             obj.role.ToString() != null && 
             obj.role.ToString() != "0")
          {
               prm[17].Value = obj.role;
          }
          else
          {
               prm[17].Value = DBNull.Value;
          }

          prm[18] = new SqlParameter("@lang", SqlDbType.NVarChar,5);
          if(obj.lang != null && 
             obj.lang.ToString() != null && 
             obj.lang.ToString() != "0")
          {
               prm[18].Value = obj.lang;
          }
          else
          {
               prm[18].Value = DBNull.Value;
          }

          prm[19] = new SqlParameter("@create_date", SqlDbType.DateTime);
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               prm[19].Value = obj.create_date;
          }
          else
          {
               prm[19].Value = DBNull.Value;
          }

          prm[20] = new SqlParameter("@create_by", SqlDbType.Int);
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               prm[20].Value = obj.create_by;
          }
          else
          {
               prm[20].Value = DBNull.Value;
          }

          prm[21] = new SqlParameter("@update_date", SqlDbType.DateTime);
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               prm[21].Value = obj.update_date;
          }
          else
          {
               prm[21].Value = DBNull.Value;
          }

          prm[22] = new SqlParameter("@update_by", SqlDbType.Int);
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               prm[22].Value = obj.update_by;
          }
          else
          {
               prm[22].Value = DBNull.Value;
          }

          prm[23] = new SqlParameter("@site_id", SqlDbType.Int);
          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               prm[23].Value = obj.site_id;
          }
          else
          {
               prm[23].Value = DBNull.Value;
          }

          prm[24] = new SqlParameter("@ReturnValue", SqlDbType.Int);
          prm[24].Direction = ParameterDirection.Output;

          DB.ExecuteNonQueryProc("sp_tcdc_mst_user_insert", prm, false);
          return (Int32)prm[24].Value;
     }

     public int updateData(tcdc_mst_user obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[25];

          prm[0] = new SqlParameter("@user_id", SqlDbType.Int);
          if(obj.user_id != null && 
             obj.user_id.ToString() != null && 
             obj.user_id.ToString() != "0")
          {
               prm[0].Value = obj.user_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }
          prm[1] = new SqlParameter("@user_type_id", SqlDbType.Int);
          if(obj.user_type_id != null && 
             obj.user_type_id.ToString() != null && 
             obj.user_type_id.ToString() != "0")
          {
               prm[1].Value = obj.user_type_id;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }
          prm[2] = new SqlParameter("@username", SqlDbType.NVarChar,50);
          if(obj.username != null && 
             obj.username.ToString() != null && 
             obj.username.ToString() != "0")
          {
               prm[2].Value = obj.username;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }
          prm[3] = new SqlParameter("@password", SqlDbType.NVarChar,50);
          if(obj.password != null && 
             obj.password.ToString() != null && 
             obj.password.ToString() != "0")
          {
               prm[3].Value = obj.password;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }
          prm[4] = new SqlParameter("@title_th", SqlDbType.Int);
          if(obj.title_th != null && 
             obj.title_th.ToString() != null && 
             obj.title_th.ToString() != "0")
          {
               prm[4].Value = obj.title_th;
          }
          else
          {
               prm[4].Value = DBNull.Value;
          }
          prm[5] = new SqlParameter("@fname_th", SqlDbType.NVarChar,250);
          if(obj.fname_th != null && 
             obj.fname_th.ToString() != null && 
             obj.fname_th.ToString() != "0")
          {
               prm[5].Value = obj.fname_th;
          }
          else
          {
               prm[5].Value = DBNull.Value;
          }
          prm[6] = new SqlParameter("@lname_th", SqlDbType.NVarChar,50);
          if(obj.lname_th != null && 
             obj.lname_th.ToString() != null && 
             obj.lname_th.ToString() != "0")
          {
               prm[6].Value = obj.lname_th;
          }
          else
          {
               prm[6].Value = DBNull.Value;
          }
          prm[7] = new SqlParameter("@title_en", SqlDbType.Int);
          if(obj.title_en != null && 
             obj.title_en.ToString() != null && 
             obj.title_en.ToString() != "0")
          {
               prm[7].Value = obj.title_en;
          }
          else
          {
               prm[7].Value = DBNull.Value;
          }
          prm[8] = new SqlParameter("@fname_en", SqlDbType.NVarChar,250);
          if(obj.fname_en != null && 
             obj.fname_en.ToString() != null && 
             obj.fname_en.ToString() != "0")
          {
               prm[8].Value = obj.fname_en;
          }
          else
          {
               prm[8].Value = DBNull.Value;
          }
          prm[9] = new SqlParameter("@lname_en", SqlDbType.NVarChar,50);
          if(obj.lname_en != null && 
             obj.lname_en.ToString() != null && 
             obj.lname_en.ToString() != "0")
          {
               prm[9].Value = obj.lname_en;
          }
          else
          {
               prm[9].Value = DBNull.Value;
          }
          prm[10] = new SqlParameter("@id_card", SqlDbType.NVarChar,50);
          if(obj.id_card != null && 
             obj.id_card.ToString() != null && 
             obj.id_card.ToString() != "0")
          {
               prm[10].Value = obj.id_card;
          }
          else
          {
               prm[10].Value = DBNull.Value;
          }
          prm[11] = new SqlParameter("@tag", SqlDbType.NVarChar,50);
          if(obj.tag != null && 
             obj.tag.ToString() != null && 
             obj.tag.ToString() != "0")
          {
               prm[11].Value = obj.tag;
          }
          else
          {
               prm[11].Value = DBNull.Value;
          }
          prm[12] = new SqlParameter("@birth_date", SqlDbType.DateTime);
          if(obj.birth_date != null && 
             obj.birth_date.ToString() != null && 
             obj.birth_date.ToString() != "0")
          {
               prm[12].Value = obj.birth_date;
          }
          else
          {
               prm[12].Value = DBNull.Value;
          }
          prm[13] = new SqlParameter("@face_image", SqlDbType.NVarChar,250);
          if(obj.face_image != null && 
             obj.face_image.ToString() != null && 
             obj.face_image.ToString() != "0")
          {
               prm[13].Value = obj.face_image;
          }
          else
          {
               prm[13].Value = DBNull.Value;
          }
          prm[14] = new SqlParameter("@gender", SqlDbType.NVarChar,1);
          if(obj.gender != null && 
             obj.gender.ToString() != null && 
             obj.gender.ToString() != "0")
          {
               prm[14].Value = obj.gender;
          }
          else
          {
               prm[14].Value = DBNull.Value;
          }
          prm[15] = new SqlParameter("@email", SqlDbType.NVarChar,50);
          if(obj.email != null && 
             obj.email.ToString() != null && 
             obj.email.ToString() != "0")
          {
               prm[15].Value = obj.email;
          }
          else
          {
               prm[15].Value = DBNull.Value;
          }
          prm[16] = new SqlParameter("@telephone", SqlDbType.NVarChar,50);
          if(obj.telephone != null && 
             obj.telephone.ToString() != null && 
             obj.telephone.ToString() != "0")
          {
               prm[16].Value = obj.telephone;
          }
          else
          {
               prm[16].Value = DBNull.Value;
          }
          prm[17] = new SqlParameter("@mobile", SqlDbType.NVarChar,50);
          if(obj.mobile != null && 
             obj.mobile.ToString() != null && 
             obj.mobile.ToString() != "0")
          {
               prm[17].Value = obj.mobile;
          }
          else
          {
               prm[17].Value = DBNull.Value;
          }
          prm[18] = new SqlParameter("@role", SqlDbType.NVarChar,20);
          if(obj.role != null && 
             obj.role.ToString() != null && 
             obj.role.ToString() != "0")
          {
               prm[18].Value = obj.role;
          }
          else
          {
               prm[18].Value = DBNull.Value;
          }
          prm[19] = new SqlParameter("@lang", SqlDbType.NVarChar,5);
          if(obj.lang != null && 
             obj.lang.ToString() != null && 
             obj.lang.ToString() != "0")
          {
               prm[19].Value = obj.lang;
          }
          else
          {
               prm[19].Value = DBNull.Value;
          }
          prm[20] = new SqlParameter("@create_date", SqlDbType.DateTime);
          if(obj.create_date != null && 
             obj.create_date.ToString() != null && 
             obj.create_date.ToString() != "0")
          {
               prm[20].Value = obj.create_date;
          }
          else
          {
               prm[20].Value = DBNull.Value;
          }
          prm[21] = new SqlParameter("@create_by", SqlDbType.Int);
          if(obj.create_by != null && 
             obj.create_by.ToString() != null && 
             obj.create_by.ToString() != "0")
          {
               prm[21].Value = obj.create_by;
          }
          else
          {
               prm[21].Value = DBNull.Value;
          }
          prm[22] = new SqlParameter("@update_date", SqlDbType.DateTime);
          if(obj.update_date != null && 
             obj.update_date.ToString() != null && 
             obj.update_date.ToString() != "0")
          {
               prm[22].Value = obj.update_date;
          }
          else
          {
               prm[22].Value = DBNull.Value;
          }
          prm[23] = new SqlParameter("@update_by", SqlDbType.Int);
          if(obj.update_by != null && 
             obj.update_by.ToString() != null && 
             obj.update_by.ToString() != "0")
          {
               prm[23].Value = obj.update_by;
          }
          else
          {
               prm[23].Value = DBNull.Value;
          }
          prm[24] = new SqlParameter("@site_id", SqlDbType.Int);
          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               prm[24].Value = obj.site_id;
          }
          else
          {
               prm[24].Value = DBNull.Value;
          }

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_user_update", prm, false);
     }

     public int deleteData(string id) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[1];

          prm[0] = new SqlParameter("@user_id", id);

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_user_delete", prm, false);
     }
}

}

